package jwdt.xadmin.info.service.impl;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.info.bean.TNews;
import jwdt.xadmin.info.service.NewsManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.image.ImageInfo;
import com.ezerg.image.ImageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.ServletActionContext;
import com.ezerg.web.cfg.WebConfig;
import com.ezerg.web.fileupload.FileItem;

public class NewsManageServiceImpl implements NewsManageService {
	
	private BaseDao<TNews> baseDao;
	private int imageZoom = WebConfig.get().getImageZoomSize();

	public void setBaseDao(BaseDao<TNews> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public String delMyNews(int newsid) throws Exception {
		return baseDao.doDelObj(TNews.class, newsid);
	}
	
	@Override
	public String saveMyNews(TNews obj) throws Exception{
		String ret = "保存成功";
		try {
			Jdb.get().add(obj);
			Jdb.get().execute("update t_user set number=number+1 where username='"+obj.getEditor()+"'");
		} catch (Exception e) {
			e.printStackTrace();
			ret = "保存出错";
		}
		return ret;
	}
	
	@Override
	public PageUtil selectNewsList(PageUtil pu,String typeid,String search,String userName,String updatetime,TNews news) throws Exception {
		String sql = "select tn.id,tn.title,tn.editor,tn.updatetime,tn.checked,tn.goodnews,tn.daodu,tn.istop,tn.picnews,ty.name,ty.id as typeid,ty.fid as fid from t_news tn,t_type ty where ty.id=tn.typeid ";
		String where = "";
		String order ="";
		if(!"".equals(typeid) && !"0".equals(typeid)) {
			where += " and tn.typeid in(" + typeid+")";
		}
		if(search!=null && !"".equals(search)) {
			where += " and title like '%" + search + "%'";
		}
		if(updatetime!=null && !"".equals(updatetime)){
			where +=" and DATE_FORMAT(tn.updatetime,'%Y-%m-%d')='"+updatetime+"'";
		}
		if(userName!=null){
			order = " and editor='" + userName + "'";
		}
		if(news!=null){
			String title = news.getTitle();
			if(!StringUtil.nullOrBlank(title)){
				where += " and title like '%" + title + "%'";
			}
			String content = news.getContent();
			if(!StringUtil.nullOrBlank(content)){
				where += " and content like '%" + content + "%'";
			}
			String author = news.getAuthor();
			if(!StringUtil.nullOrBlank(author)){
				where += " and author like '%" + author + "%'";
			}
			String starttime = news.getStarttime();
			if(!StringUtil.nullOrBlank(starttime)){
				where += " and starttime >'" + starttime + "'";
			}
			String stoptime = news.getStoptime();
			if(!StringUtil.nullOrBlank(stoptime)){
				where += " and stoptime <'" + stoptime + "'";
			}
			Integer picnews = news.getPicnews();
			if(picnews !=null){
				where += " and picnews =" +picnews;
			}
			Integer goodnews = news.getGoodnews();
			if(goodnews !=null){
				where += " and goodnews =" + goodnews;
			}
			Integer daodu = news.getDaodu();
			if(daodu !=null){
				where += " and daodu =" + daodu;
			}
			Integer istop = news.getIstop();
			if(istop !=null){
				where += " and istop =" + istop;
			}
			Integer checked = news.getChecked();
			if(checked !=null){
				where += " and checked =" + checked;
			}
		}
		order+=" order by UpdateTime desc";
		pu.doList(sql+where+order);
		return pu;
	}
	
	@Override
	public String newsEdit(int newsid,TNews obj) throws Exception {
		return baseDao.doUpdateObj(TNews.class, newsid, obj);
	}
	
	@Override
	public String newsChkact(int newsid,int checked) throws Exception {
		return baseDao.doIntUpdateState(TNews.class, "id="+newsid,"checked="+checked);
	}
	
	@Override
	public String newsGood(int newsid,int goodnews) throws Exception {
		return baseDao.doIntUpdateState(TNews.class, "id="+newsid,"goodnews="+goodnews);
	}
	
	@Override
	public String newsTop(int newsid,int istop) throws Exception {
		return baseDao.doIntUpdateState(TNews.class, "id="+newsid,"istop="+istop);
	}
	
	@Override
	public String newsDaodu(int newsid,int daodu) throws Exception {
		return baseDao.doIntUpdateState(TNews.class, "id="+newsid, "daodu="+daodu);
	}
	
	@Override
	public String doAddact(FileItem file) throws Exception {
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + "uploadfile/";
		String ret = "添加成功";
		try {
			if(file != null) {
				String fileExt = file.getFileExt();
				String fileName = DateUtil.getComplexDateTime() + "." + fileExt;
				String absolutePath = uploadPath + fileName;
				//
				ImageInfo ii = new ImageInfo();
				ii.setInput(file.getInputStream());
				//System.out.println(imageZoom);
				if(ii.check() && imageZoom>0 && ii.getWidth()>imageZoom || ii.getHeight()>imageZoom) {//图片
					try {// 图片缩放功能
						BufferedImage image = ImageUtil.zoom(file.getInputStream(), imageZoom, imageZoom);
						ImageIO.write(image, ii.getFormatName(), new FileOutputStream(absolutePath));
						
					} catch (Exception e) {
						e.printStackTrace();
						// 缩放失败则直接写入
						file.write(absolutePath);
					}
					ret = fileName;
				} else {//非图片文件则直接写入
					file.write(absolutePath);
				}
				ret = fileName;
			}else{
				ret = "请重新上传文件";
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		return ret;
	}

}
